<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <tree-tools :tree-node="company" :is-root="true" />

        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <tree-tools
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="loadDepartments"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
    </div>
    <!-- 增加部门的窗体 -->
    <add-dept
      ref="addDept"
      :show-dialog.sync="showDialog"
      :tree-node="node"
      @addDepts="loadDepartments"
    />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools'
import { getDepartments } from '@/api/departments'
import { rsolveList2Tree } from '@/utils/index'
import AddDept from './components/add-dept'

export default {
  name: 'Departments',
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      loading: false,
      showDialog: false,
      company: {
        name: '',
        manager: ''
      },
      defaultProps: {
        label: 'name'
      },
      departs: [],
      node: {}
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    editDepts(node) {
      this.node = node
      this.$refs.addDept.getDepartmentDetail(node.id)
      this.showDialog = true
    },
    addDepts(node) {
      this.showDialog = true
      this.node = node
    },
    async loadDepartments() {
      this.loading = true
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      // this.departs = tranListToTreeData(result.depts, '')
      this.departs = rsolveList2Tree(result.depts)
      this.loading = false
      // console.log(this.departs)
    }
  }
}
</script>

<style>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
